/*
 * @Author: Drswith
 * @Date: 2021-01-11 18:53:23
 * @LastEditors: Drswith
 * @LastEditTime: 2021-01-11 19:49:16
 * @FilePath: \device-3-dht11\device\device.ino
 */
/*    头文件      */
#include "Button2.h";
#include "DHTesp.h"

/*    引脚号宏定义      */
#define PIN_BEEP      13
#define PIN_LED       2
#define PIN_KEY_LED   14
#define PIN_KEY_BEEP  12
#define PIN_DHT11     0

/*    全局变量（对象）      */
unsigned long previousMillis = 0; 
const long interval = 2000;
int Pin_state = LOW; 

Button2 led_key = Button2(PIN_KEY_LED);
Button2 beep_key = Button2(PIN_KEY_BEEP);
DHTesp dht;

/*    初始化函数      */
void setup() {
  Serial.begin(115200);
  delay(50);
  Serial.println("\n\nSave Tiro Season2 NodeMcu Arduino IDE Weapp\n\n");
  
  pinMode(PIN_LED, OUTPUT);
  pinMode(PIN_BEEP, OUTPUT);
  led_key.setTapHandler(led_tap);
  beep_key.setTapHandler(beep_tap);

  dht.setup(PIN_DHT11, DHTesp::DHT11); // Connect DHT sensor to PIN_DHT11
}


/*    主循环函数      */
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (Pin_state == LOW) {
      Pin_state = HIGH;
    } else {
      Pin_state = LOW;
    }
    digitalWrite(PIN_LED, Pin_state);  
    digitalWrite(PIN_BEEP, Pin_state);

    float humidity = dht.getHumidity();
    float temperature = dht.getTemperature();

    Serial.print(dht.getStatusString());
    Serial.print("\t");
    Serial.print(humidity, 1);
    Serial.print("\t\t");
    Serial.print(temperature, 1);
    Serial.print("\t\t");
  }
  led_key.loop();
  beep_key.loop();                   
}

void led_tap(Button2& btn) {
    Serial.println("led_key tap");
}
void beep_tap(Button2& btn) {
    Serial.println("beep_key tap");
}
